package br.com.californiahotel.server.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import br.com.californiahotel.client.service.HospedeService;
import br.com.californiahotel.server.hibernate.DatabaseUtils;
import br.com.californiahotel.server.hibernate.models.Hospede;
import br.com.californiahotel.server.utils.Conversor;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class HospedeServiceImpl extends RemoteServiceServlet implements
HospedeService {

	private static final Class<Hospede> HOSPEDE_CLASS = br.com.californiahotel.server.hibernate.models.Hospede.class;
	private static final DatabaseUtils<Hospede> DATABASE = new DatabaseUtils<Hospede>();
	private static final Conversor CONVERSOR = new Conversor();

	@Override
	public boolean save(br.com.californiahotel.shared.models.Hospede model) {
		try {
			DATABASE.save((Hospede)CONVERSOR.convertModelToHibernate( model, HOSPEDE_CLASS));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	@Override
	public boolean edit(br.com.californiahotel.shared.models.Hospede model, String id) {
		try {
			DATABASE.removeById(HOSPEDE_CLASS, id);
			DATABASE.save((Hospede)CONVERSOR.convertModelToHibernate( model, HOSPEDE_CLASS));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List<br.com.californiahotel.shared.models.Hospede> findAll() {
		ArrayList<br.com.californiahotel.shared.models.Hospede> HospedeList = new ArrayList<br.com.californiahotel.shared.models.Hospede>();
		List<Hospede> findAll = DATABASE.findAll(HOSPEDE_CLASS);
		for (Hospede Hospede : findAll) {
			HospedeList.add((br.com.californiahotel.shared.models.Hospede) CONVERSOR.convertHibernateToModel(Hospede, br.com.californiahotel.shared.models.Hospede.class));
		}
		return HospedeList;
	}

	@Override
	public boolean delete(String key) {
		try {
			DATABASE.removeById(HOSPEDE_CLASS, key);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

}
